@use 'sass:map';

@use './shared' as *;
@use './design' as *;

$upload: () !default;
$upload: map.merge(
  (
    color-success: get-css-var('color-success-base'),
    color-fail: get-css-var('color-error-base'),
    color-loading: get-css-var('content-color-disabled'),
    panel-bg-color: get-css-var('fill-color-base'),
    panel-b-width: get-css-var('border-width'),
    panel-b-style: dashed,
    panel-b-color: get-css-var('border-color-base'),
    panel-b-color-hover: get-css-var('color-primary-base'),
    panel-b-color-focus: get-css-var('upload-panel-b-color-hover'),
    panel-b-color-over: get-css-var('color-primary-base'),
    panel-b-color-disabled: get-css-var('border-color-light-2'),
    panel-radius: get-css-var('radius-base'),
    cloud-color: get-css-var('content-color-secondary'),
    cloud-color-over: get-css-var('color-primary-base'),
    cloud-color-disabled: get-css-var('content-color-disabled'),
    file-bg-color: transparent,
    file-bg-color-hover: get-css-var('fill-color-hover'),
    file-b-color: get-css-var('border-color-base'),
    file-b-color-success: get-css-var('color-success-base'),
    file-b-color-fail: get-css-var('color-error-base'),
    file-radius: get-css-var('radius-base'),
    mask-bg-color: get-css-var('color-black'),
    mask-opacity: 45%,
    action-color: get-css-var('content-color-third'),
    action-color-hover: get-css-var('content-color-primary'),
    action-color-disabled: get-css-var('content-color-disabled'),
    thumb-color: get-css-var('content-color-secondary'),
    thumb-size: 70px,
    thumb-action-color: rgba(255, 255, 255, 70%),
    thumb-action-color-hover: rgba(255, 255, 255, 100%),
    thumb-action-color-disabled: rgba(255, 255, 255, 30%),
    card-thumb-size: 56px,
    card-action-color: get-css-var('content-color-secondary'),
    card-action-color-hover: get-css-var('color-primary-base'),
    card-action-color-disabled: get-css-var('content-color-disabled'),
    percentage-color: get-css-var('content-color-secondary')
  ),
  $upload
);

.#{$namespace}-upload {
  &-vars {
    @include define-preset-values('upload', $upload);
  }

  @mixin define-upload-style($style-map) {
    @include define-preset-style('upload', $style-map);
  }

  @include basis;

  position: relative;
  display: inline-flex;
  flex-direction: column;
  align-items: flex-start;

  &--to-add {
    flex-direction: column-reverse;
  }

  &--block {
    width: 100%;
  }

  $states: success, error, warning;

  @each $state in $states {
    &--#{$state} {
      @include define-upload-style(
        (
          panel-b-color: 'color' $state 'opacity-3',
          panel-b-color-hover: 'color' $state 'base',
          panel-b-color-over: 'color' $state 'base',
          cloud-color: 'color' $state 'opacity-2',
          cloud-color-over: 'color' $state 'base'
        )
      );
    }
  }

  &__input {
    display: none;
  }

  &__control {
    display: flex;
    flex-direction: column;
  }

  &--drag,
  &--drag &__control {
    width: 100%;
  }

  &__image-control {
    position: relative;
    display: inline-flex;
    align-items: center;
    width: auto;
    padding: 6px;
    margin-inline-end: 6px;
    margin-bottom: 6px;
    overflow: hidden;
    vertical-align: middle;
    cursor: pointer;
    border: get-css-var('upload-panel-b-width') get-css-var('upload-panel-b-style')
      get-css-var('upload-panel-b-color');
    border-radius: get-css-var('upload-file-radius');
    outline: 0;
    transition: get-css-var('transition-color'), get-css-var('transition-border');

    &:hover,
    &--drag-over {
      border-color: get-css-var('upload-panel-b-color-hover');
    }

    &:focus {
      border-color: get-css-var('upload-panel-b-color-focus');
    }

    &--disabled {
      color: get-css-var('content-color-disabled');
      cursor: not-allowed;

      &,
      &:hover,
      &:focus {
        border-color: get-css-var('upload-panel-b-color-disabled');
      }
    }
  }

  &__image-action {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: get-css-var('upload-thumb-size');
    height: get-css-var('upload-thumb-size');
    color: get-css-var('upload-thumb-color');

    &--disabled {
      color: get-css-var('content-color-disabled');
    }
  }

  &__drag-panel {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100%;
    padding: 20px;
    overflow: hidden;
    cursor: pointer;
    background-color: get-css-var('upload-panel-bg-color');
    border: get-css-var('upload-panel-b-width') get-css-var('upload-panel-b-style')
      get-css-var('upload-panel-b-color');
    border-radius: get-css-var('upload-panel-radius');
    outline: 0;
    transition: get-css-var('transition-color'), get-css-var('transition-border');

    &:hover {
      border-color: get-css-var('upload-panel-b-color-hover');
    }

    &:focus {
      border-color: get-css-var('upload-panel-b-color-focus');
    }

    &--disabled {
      color: get-css-var('content-color-disabled');
      cursor: not-allowed;

      &,
      &:hover,
      &:focus {
        border-color: get-css-var('upload-panel-b-color-disabled');
      }
    }
  }

  &--drag-only &__drag-panel {
    cursor: auto;
  }

  &__control--drag-over &__drag-panel {
    border-color: get-css-var('upload-panel-b-color-over');

    &--disabled {
      border-color: get-css-var('upload-panel-b-color-disabled');
    }
  }

  &__cloud {
    color: get-css-var('upload-cloud-color');
    transition: get-css-var('transition-color');

    &--disabled {
      color: get-css-var('upload-cloud-color-disabled');
    }
  }

  &__control--drag-over &__cloud,
  &__image-control--drag-over &__cloud {
    color: get-css-var('upload-cloud-color-over');

    &--disabled {
      color: get-css-var('upload-cloud-color-disabled');
    }
  }

  &__loading-icon {
    pointer-events: none;
    transition: get-css-var('transition-opacity');
  }

  &__tip {
    margin: 4px 0;
  }

  &__drag-panel &__tip {
    margin: 8px 0 4px;
    user-select: none;
  }

  &__files {
    @include basis;

    position: relative;
    display: flex;
    flex-wrap: wrap;
    width: 100%;
    padding: 0;
    margin: 0;
  }

  &--image &__files {
    margin-bottom: -8px;
  }

  &--has-file:not(&--image) &__files {
    margin: 0;
    margin-top: 8px;
  }

  &--to-add:not(&--image)#{&}--has-file &__files {
    margin: 0;
    margin-bottom: 8px;
  }

  &__file {
    @include basis;

    position: relative;
    display: inline-flex;
    align-items: center;
    vertical-align: middle;
    list-style: none;
    border-radius: get-css-var('upload-file-radius');
    transition: get-css-var('transition-transform'), get-css-var('transition-opacity');

    &--name {
      justify-content: space-between;
      width: 100%;
      padding: 3px 6px;
      margin-bottom: 3px;
      background-color: get-css-var('upload-file-bg-color');
      transition: get-css-var('transition-background');

      &:hover,
      &:focus,
      &:focus-within {
        background-color: get-css-var('upload-file-bg-color-hover');
      }

      &:last-child {
        margin-bottom: 0;
      }
    }

    &--thumbnail,
    &--card {
      padding: 6px;
      margin-bottom: 6px;
      border: get-css-var('border-shape') get-css-var('upload-file-b-color');
      transition: get-css-var('transition-border');
    }

    &--thumbnail {
      margin-inline-end: 6px;
    }

    &--thumbnail#{&}--success,
    &--card#{&}--success {
      border-color: get-css-var('upload-file-b-color-success');
    }

    &--thumbnail#{&}--fail,
    &--card#{&}--fail {
      border-color: get-css-var('upload-file-b-color-fail');
    }

    &--card {
      width: 100%;
    }
  }

  &__filename {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    transition: get-css-var('transition-color');
  }

  &__file--success &__filename {
    color: get-css-var('upload-color-success');
  }

  &__file--fail &__filename {
    color: get-css-var('upload-color-fail');
  }

  &__label {
    display: flex;
    align-items: center;
    margin-inline-end: 14px;
    user-select: none;
  }

  &__card {
    position: relative;
    display: flex;
    align-items: center;

    * {
      flex-shrink: 0;
    }
  }

  &__file--thumbnail &__card {
    justify-content: center;
  }

  &__file--card &__card {
    width: 100%;
  }

  &__thumbnail {
    display: flex;
    align-items: center;
    justify-content: center;
    color: get-css-var('upload-thumb-color');
  }

  &__file--thumbnail &__thumbnail {
    width: get-css-var('upload-thumb-size');
    height: get-css-var('upload-thumb-size');
  }

  &__file--card &__thumbnail {
    width: get-css-var('upload-card-thumb-size');
    height: get-css-var('upload-card-thumb-size');
    margin-inline-end: 14px;
  }

  &__info {
    display: flex;
    flex-direction: column;
    justify-content: center;
    width: calc(100% - #{get-css-var('upload-card-thumb-size')} - 14px);
    padding-inline-end: calc(#{get-css-var('upload-card-thumb-size')} + 14px);
  }

  &__file--card &__filename {
    width: 100%;
  }

  &__image {
    width: 100%;
    height: 100%;
    user-select: none;
    object-fit: cover;
  }

  &__actions {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  &__file--thumbnail &__actions {
    position: absolute;
    top: 0;
    inset-inline-start: 0;
    width: 100%;
    height: 100%;
    opacity: 0%;
    transition: get-css-var('transition-opacity');
  }

  &__file--thumbnail &__mask {
    position: absolute;
    top: 0;
    inset-inline-start: 0;
    width: 100%;
    height: 100%;
    background-color: get-css-var('upload-mask-bg-color');
    opacity: get-css-var('upload-mask-opacity');
  }

  &__card:hover &__actions,
  &__card:focus &__actions,
  &__card:focus-within &__actions {
    opacity: 100%;
  }

  &__file--card &__actions {
    position: absolute;
    inset-inline-end: 0;
  }

  &__action {
    position: relative;
    padding: 0;
    margin: 0 4px;
    cursor: pointer;
    background-color: transparent;
    border: 0;
    outline: 0;

    &--disabled {
      color: get-css-var('upload-action-color-disabled');
      cursor: not-allowed;
    }
  }

  &__file--thumbnail &__action {
    color: get-css-var('upload-thumb-action-color');

    &:hover,
    &:focus {
      color: get-css-var('upload-thumb-action-color-hover');
    }

    &--disabled {
      &,
      &:hover,
      &:focus {
        color: get-css-var('upload-thumb-action-color-disabled');
      }
    }
  }

  &__file--card &__action {
    color: get-css-var('upload-card-action-color');

    &:hover,
    &:focus {
      color: get-css-var('upload-card-action-color-hover');
    }

    &--disabled {
      &,
      &:hover,
      &:focus {
        color: get-css-var('upload-card-action-color-disabled');
      }
    }
  }

  &__icon {
    display: flex;
    align-items: center;
    justify-content: center;
    transition: get-css-var('transition-color');
  }

  &__file-icon {
    width: 16px;
    margin-inline-end: 4px;
  }

  &__success,
  &__fail,
  &__loading,
  &__close {
    width: 14px;
  }

  &__close {
    position: absolute;
    inset-inline-end: 0;
    padding: 0;
    cursor: pointer;
    background-color: transparent;
    border: 0;
    outline: 0;

    &:hover,
    &:focus {
      color: get-css-var('upload-action-color-hover');
    }
  }

  &__close,
  &__file:hover &__success,
  &__file:hover &__fail,
  &__file:hover &__loading {
    color: transparent;
  }

  &__file:hover &__close {
    color: get-css-var('upload-action-color');

    &:hover,
    &:focus {
      color: get-css-var('upload-action-color-hover');
    }
  }

  &__success {
    color: get-css-var('upload-color-success');
  }

  &__fail {
    color: get-css-var('upload-color-fail');
  }

  &__loading {
    color: get-css-var('upload-color-loading');
  }

  &__progress {
    display: flex;
    align-items: center;
    justify-content: center;
    user-select: none;
  }

  &__file--name &__progress {
    position: absolute;
    inset-inline-end: 6px;
    bottom: 0;
    inset-inline-start: 6px;
  }

  &__file--thumbnail &__progress {
    flex-direction: column;
    width: 100%;
    height: 100%;
    padding: 6px;
  }

  &__file--card &__progress {
    padding-top: 4px;
  }

  &__percentage {
    color: get-css-var('upload-percentage-color');
  }
}
